define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'basic/assets/index' + location.search,
                    add_url: 'basic/assets/add',
                    edit_url: 'basic/assets/edit',
                    del_url: 'basic/assets/del',
                    multi_url: 'basic/assets/multi',
                    import_url: 'basic/assets/import',
                    table: 'assets',

                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                searchFormVisible: true,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: 'ID'},
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'number', title: __('Number'), operate: 'LIKE'},
                        {field: 'initial_amount', title: __('Initial_amount'), operate: 'BETWEEN'},
                        {field: 'month_disburse', title: __('本月支出'), operate: false},
                        {field: 'month_income', title: __('本月收入'), operate: false},
                        {field: 'balance', title: __('Balance'), operate: 'BETWEEN'},
                        //     {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        //     {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('记录'),
                                    title: __('记录'),
                                    extend: 'data-area=["95%","95%"]',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'basic/assets/assets_log',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        select: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'basic/assets/index' + location.search,
                    add_url: 'basic/assets/add',
                    // edit_url: 'basic/assets/edit',
                    // del_url: 'basic/assets/del',
                    multi_url: 'basic/assets/multi',
                    import_url: 'basic/assets/import',
                    table: 'assets',
                    dragsort_url: "",
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                searchFormVisible: true,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: 'ID'},
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'number', title: __('Number'), operate: 'LIKE'},
                        {field: 'initial_amount', title: __('Initial_amount'), operate: false},
                        {field: 'balance', title: __('Balance'), operate: false},

                        //     {field: 'weigh', title: __('Weigh'), operate: false},
                        // {
                        //     field: 'createtime',
                        //     title: __('Createtime'),
                        //     operate: 'RANGE',
                        //     addclass: 'datetimerange',
                        //     autocomplete: false,
                        //     formatter: Table.api.formatter.datetime
                        // },
                        //     {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'use',
                                    text: __('选择'),
                                    title: __('选择'),
                                    classname: 'btn btn-xs btn-warning btn-click',
                                    click: function (e, row) {
                                        //获取选中的id，获取到的是个数组
                                        Fast.api.close(row);//这里是重点，将这个ids 传至父页面
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        assets_log: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'basic/assets/assets_log' + location.search,
                    add_url: 'basic/assets/add',
                    edit_url: 'basic/assets/edit',
                    del_url: 'basic/assets/del',
                    multi_url: 'basic/assets/multi',
                    import_url: 'basic/assets/import',
                    table: 'assets',

                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                searchFormVisible: true,
                columns: [
                    [
                        //  {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {
                            field: 'assets.name',
                            title: __('账户'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'order_no', title: __('单据号'), operate: 'LIKE'},
                        {
                            field: 'status',
                            title: __('类型'),
                            searchList: {
                                "100": __('销售单'),
                                "200": __('销售退货单'),
                                "300": __('零售单'),
                                "400": __('进货单'),
                                "500": __('进货退货单'),
                                "600": __('收款单'),
                                "700": __('付款单'),
                                "800": __('项目收入单'),
                                "900": __('项目支出单'),
                                "1000": __('转账单'),
                                "9900": __('系统修改'),
                                "10000": __('系统删除销售单'),
                                "20000": __('系统删除销售退货单'),
                                "40000": __('系统删除进货单'),
                                "50000": __('系统删除退货单'),
                            },
                            formatter: Table.api.formatter.status
                        },
                        {field: 'staff_id', title: __('经手人'), operate: false},
                        {field: 'price', title: __('金额'), operate: 'BETWEEN'},
                        {field: 'balance', title: __('余额'), operate: false},
                        //     {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        //     {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {
                        //     field: 'operate',
                        //     title: __('Operate'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     formatter: Table.api.formatter.operate,
                        //     buttons: [
                        //         {
                        //             name: 'detail',
                        //             text: __('记录'),
                        //             title: __('记录'),
                        //             extend: 'data-area=["95%","95%"]',
                        //             classname: 'btn btn-xs btn-primary btn-dialog',
                        //             url: 'basic/assets/assets_log',
                        //             callback: function (data) {
                        //                 Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                        //             },
                        //             visible: function (row) {
                        //                 //返回true时按钮显示,返回false隐藏
                        //                 return true;
                        //             }
                        //         },
                        //     ]
                        // }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'basic/assets/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'basic/assets/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'basic/assets/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
